import Vue from "vue";
import pako from "pako";
import {exportJsonToExcel} from "../excel/Export2Excel";
import {closeGlobalLoading, globalLoading} from "../loading";
import cache from "@/method/cache";

export default {
  install() {
    /** 压缩  */
    Vue.prototype.$zip = function (str) {
      const binaryString = pako.gzip(encodeURIComponent(str), {to: 'string'})
      return btoa(binaryString);
    };
    /** 解压缩  */
    Vue.prototype.$unzip = function (b64Data) {
      const strData1 = atob(b64Data);
      // 将二进制字符串转换为字符数数组
      const charData = strData1.split("").map(function (x) {
        return x.charCodeAt(0);
      });
      // 把数字数组转换成字节数组
      const binData = new Uint8Array(charData);
      // 解压
      const data = pako.inflate(binData);
      //将解压缩完成的的byteArray转换回ascii字符串:
      const strData2 = new TextDecoder("utf-8").decode(data);//大数据使用此方法
      //const strData   = String.fromCharCode.apply(null, new Uint16Array(data));//数据不是很多可以使用此方法
      return decodeURIComponent(strData2);
    };
    /** 分页方法 */
    Vue.prototype.$getPageInfo = function (pageSize, pageNum) {
      return {
        "pageSize": pageSize,
        "pageNum": pageNum
      }
    };
    /** 后端返回的分页信息 */
    Vue.prototype.$cutPageDataList = function (response) {
      return {
        "dataList": response.list,
        "totalPage": response.total
      }
    };
    /** 将observer转换为正常对象 */
    Vue.prototype.$ObserverToObject = function (data) {
      return Object.assign([], data).map(item => {
        return item
      })
    };
    /** 字符串转JSON */
    Vue.prototype.$StrToJson = function (res) {
      const type = [];
      res.forEach(r => type.push(eval('(' + r["dict"] + ')')))
      return type;
    };
    /** 赋值对象 */
    Vue.prototype.$CopyObject = function (res) {
      const copyObject = {};
      for (const i in res) {
        copyObject[i] = res[i]
      }
      return copyObject;
    };
    /** 下载文件 */
    Vue.prototype.$DownloadFile = function (name, stream) {
      const bstr = atob(stream);
      let n = bstr.length;
      const u8arr = new Uint8Array(n)
      while (n--) {
        u8arr[n] = bstr.charCodeAt(n)
      }
      const blob = new Blob([u8arr], {type: 'application/vnd.ms-excel'})
      const link = document.createElement('a')
      link.style.display = 'none'
      link.href = URL.createObjectURL(blob)
      link.setAttribute('download', name)
      document.body.appendChild(link)
      link.click()
      this.loading = false
    };
    /** 获取分页尺寸 */
    Vue.prototype.$getPageSize = function () {
      return eval(sessionStorage.getItem("pageSize"))
    };
    /** 实体列表根据字段名称转为json */
    Vue.prototype.$formatJson = function (filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]));
    };
    //表格数据写入excel
    Vue.prototype.$exportExcel = function (res) {
      const tHeader = res["headers"]; // 导出的excel表头名信息
      const filterVal = res["names"]; // 导出的excel表头字段名，需要导出表格字段名
      const data = this.$formatJson(filterVal, res["list"]);
      exportJsonToExcel(tHeader, data, res["fileName"]); // 导出的表格名称，根据需要自己命名
    };
    //设置全局设置
    Vue.prototype.$setGlobalConfig = function (res) {
      for (let key in res) {
        //页号
        if (key === "pageSize") {
          const page = []
          res["pageSize"].forEach(item => {
            page.push(Number(item.value))
          })
          page.sort(function (a, b) {
            return a - b;
          });
          sessionStorage.setItem("pageSize", "[" + page + "]");
        }
        //hot
        if (key === "hot") {
          const value = res["hot"][0]["value"]
          sessionStorage.setItem("hot", value);
        }
      }
    };
    //全局loading
    Vue.prototype.$GlobalLoading = function (b, options) {
      if (Boolean(b)) {
        globalLoading(options)
      } else {
        closeGlobalLoading()
      }
    }
    // 缓存对象
    Vue.prototype.$cache = cache
  }
}
